<template>
	<view class="myNeeds">
		<view class="" style="background: #fff;">
			<NavBar title="我的需求(买方)">
				<template #right>
					<image class="topRight" src="/static/img/index/155.png" mode="widthFix"></image>
				</template>
			</NavBar>
		</view>
		<view class="top">
			<view :class="data.obj.state == ''?'active':''" @tap="data.obj.state = '',data.obj.pageNo = 1,requstList()">
				全部
			</view>
			<view :class="data.obj.state == 'HIDE'?'active':''" @tap="data.obj.state = 'HIDE',data.obj.pageNo = 1,requstList()">
				未上架
			</view>
			<view :class="data.obj.state == 'SHOW'?'active':''" @tap="data.obj.state = 'SHOW',data.obj.pageNo = 1,requstList()">
				已上架
			</view>
			<view :class="data.obj.state == 'ORDER_SHOW'?'active':''" @tap="data.obj.state = 'ORDER_SHOW',data.obj.pageNo = 1,requstList()">
				进行中
			</view>
			<view :class="data.obj.state == 'COMPLETE'?'active':''" @tap="data.obj.state = 'COMPLETE',data.obj.pageNo = 1,requstList()">
				已完成
			</view>
		</view>
		<scroll-view @scroll="handeScroll" @refresherrefresh="refresherrefresh" @scrolltolower="scrolltolower" :refresher-triggered="data.refresherBool" :refresher-enabled="true" scroll-y="true">
			<view class="list" @touchstart="touchstart">
				<view @longpress="longpress(item,index)" class="listItem" v-for="(item,index) in data.result.list" :key="index">
					<view class="top">
						创建时间:{{item.createTime}}
						<text :class="item.state == 'SHOW'?'text-up':item.state == 'ORDER_SHOW'?'text-ordershow':item.state == 'COMPLETE'?'text-complete':'text-down'">
							{{item.state == 'SHOW'?'已上架':item.state == 'ORDER_SHOW'?'进行中':item.state == 'COMPLETE'?'已完成':'未上架'}}</text>
					</view>
					<view class="goods">
						<view class="left">
							<text>{{item.sellType == 'OFFLINE'?'线下':'线上'}}</text>
							<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item.attachmentIds[0]" mode="aspectFill"></image>
						</view>
						<view class="right">
							{{item.title}}
							<text>{{item.content}}</text>
							<view class="priceText">
								<text>{{item.price.split('.')[0]}}</text>
								<text>.{{item.price.split('.')[1]}}</text>
							</view>
						</view>
					</view>
					<view class="bot" v-if="item.state == 'SHOW' || item.state == 'HIDE'">
						<text @tap="handeEdit(item)">编辑</text>
						<text @tap="handeClick(item,index)" :class="item.state == 'SHOW'?'text-up':'text-down'">{{item.state == 'SHOW'?'下架':'上架'}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="bot">
			<text @tap="navTo('/pages/releaseRequirements/index')">发需求(买方)</text>
		</view>
	</view>
</template>

<script setup lang="ts">
	import NavBar from '@/components/navBar.vue'
	import { reactive } from 'vue';
	import { queryVpsDemandList, dleVpsDemand, updateVpsState } from '@/utils/api/map'
	const data = reactive<any>({
		httpUrl: getApp().globalData.baseUrl,
		result: {},
		obj: { pageNo: 1, pageSize: 10, state: '' }
	})
	uni.$off('myNeeds')
	uni.$on('myNeeds', () => {
		data.obj.pageNo = 1;
		requstList()
	})
	const refresherrefresh = () => {
		data.refresherBool = true;
		data.obj.pageNo = 1;
		requstList()
	}
	const handeScroll = () => {
		data.bool = false;
	}
	const touchstart = () => {
		data.bool = true;
	}
	const handeClick = (item, index) => {
		uni.showModal({
			title: '提示',
			content: item.state == 'SHOW' ? '是否下架?' : "是否上架?",
			success: (e) => {
				if (e.confirm) {
					uni.showLoading({
						title: '加载中...',
						mask: true
					})
					updateVpsState({ data: { state: item.state == 'SHOW' ? 'HIDE' : 'SHOW', id: item.id } }).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '操作成功!',
								icon: 'none'
							})
							if (data.obj.state) {
								data.result.list.splice(index, 1)
							} else {
								item.state = item.state == 'SHOW' ? 'HIDE' : 'SHOW'
							}
						}
					})
				}
			}
		})
	}
	const requstList = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		if (data.obj.pageNo == 1) {
			data.result.list = [];
		}
		queryVpsDemandList({ data: data.obj }).then((res) => {
			res.data.list.map((item) => {
				item.price = item.price + ''
				if (item.price.indexOf('.') == -1) {
					item.price = item.price + '.00'
				}
				if (item.attachmentIds) {
					item.attachmentIds = item.attachmentIds.split(',')
				}
			})
			data.refresherBool = false;
			res.data.list = [...data.result.list, ...res.data.list];
			data.result = res.data;
			uni.hideLoading()
		})
	}
	const longpress = (item, index) => {
		if (data.bool && (item.state == 'SHOW' || item.state == 'HIDE')) {
			uni.showActionSheet({
				itemList: ['删除'],
				success: (e) => {
					uni.showModal({
						title: '提示',
						content: '是否删除该需求?',
						success: (e) => {
							if (e.confirm) {
								uni.showLoading({
									title: '加载中...',
									mask: true
								})
								dleVpsDemand({ data: { ids: [item.id] } }).then((res) => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功!',
											icon: 'none'
										})
										data.result.list.splice(index, 1)
									}
								})
							}
						}
					})
				}
			})
		}

	}

	const handeEdit = (item) => {
		uni.navigateTo({
			url: '/pages/releaseRequirements/index?id=' + item.id
		})
	}
	const scrolltolower = () => {
		if (data.result.total != data.result.list.length) {
			data.obj.pageNo += 1;
			requstList()
		}
	}
	requstList()
	const navTo = (url) => {
		uni.navigateTo({
			url
		})
	}
</script>

<style lang="scss">
	.text-up {
		border-color: #e1f3d8;
		background: #e1f3d8;
		color: #09bb07;
	}

	.text-down {
		border-color: #f2f6fc;
		background: #f2f6fc;
		color: #909399;
	}

	.text-ordershow {
		border-color: #f2f6fc;
		background: #f2f6fc;
		color: #909399;
	}

	.text-complete {
		border-color: #f2f6fc;
		background: #f2f6fc;
		color: #909399;
	}

	.myNeeds {
		height: 100vh;
		background: #F4F6FA;
		display: flex;
		flex-direction: column;

		scroll-view {
			padding: 24rpx;
			flex: 1;
			overflow: auto;
		}

		.list {
			>view:nth-of-type(1) {
				margin-top: 0;
			}
		}

		.listItem {
			padding: 0 20rpx;
			margin-top: 24rpx;
			background: #fff;
			border-radius: 16rpx;
			padding-bottom: 24rpx;

			>.top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #595959;
				// text {
				// 	color: #FF8C00;
				// }
				padding: 20rpx 0;
			}

			>.goods {
				display: flex;
				align-items: center;

				.left {
					position: relative;
					width: 140rpx;
					height: 140rpx;
					border-radius: 24rpx;
					background: #ccc;
					margin-right: 16rpx;

					text {
						width: 72rpx;
						line-height: 40rpx;
						text-align: center;
						font-size: 20rpx;
						color: #FFF5E2;
						display: inline-block;
						position: absolute;
						left: 0;
						top: 0;
						box-shadow: inset -1rpx -1rpx 4rpx 0rpx rgba(255, 234, 160, 0.3);
						border-radius: 24rpx 0 24rpx 0;
						background: linear-gradient(223deg, #CD9A67 0%, #C69769 100%);
					}

					image {
						width: 100%;
						height: 100%;
						border-radius: 24rpx;
					}
				}

				.right {
					flex: 1;
					width: 0;
					font-size: 32rpx;
					display: flex;
					flex-direction: column;

					>text {
						color: #595959;
						font-size: 24rpx;
						margin-top: 8rpx;
					}
				}
			}

			>.bot {
				margin-top: 24rpx;
				border-top: 2rpx solid #F4F6FA;
				display: flex;
				justify-content: flex-end;
				padding: 24rpx 0 0 0;

				text {
					width: 140rpx;
					line-height: 64rpx;
					text-align: center;
					border-radius: 32rpx;
					margin-left: 16rpx;
				}

				text:nth-of-type(1) {
					color: #595959;
					border: 2rpx solid #D9D9D9;
				}

				// text:nth-of-type(2) {
				// 	color: #6B64D3;
				// 	border: 2rpx solid #6B64D3;
				// }
			}
		}

		>.top {
			background: #fff;
			display: flex;
			justify-content: space-between;
			padding: 38rpx 40rpx 0 40rpx;

			view {
				color: #595959;
				display: flex;
				flex-direction: column;
				align-items: center;

				&::after {
					margin-top: 20rpx;
					height: 4rpx;
					border-radius: 4rpx;
					width: 40rpx;
					content: '';
					display: inline-block;
				}
			}

			.active {
				color: #6B64D3;

				&:after {
					background: #6B64D3;
				}
			}
		}

		>.bot {
			padding: 8rpx 32rpx 20rpx 32rpx;
			background: #fff;

			text {
				display: inline-block;
				width: 100%;
				line-height: 80rpx;
				text-align: center;
				font-size: 32rpx;
				color: #fff;
				border-radius: 80rpx;
				background: #6B64D3;
			}
		}
	}

	.topRight {
		width: 48rpx;
	}
</style>